<?php

class oauth_model extends jqxGrid_CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    function getByEmail($email){
        $where=array("Email"=>$email);
        $query=$this->db->get_where('adp_oauth', $where); 
        return $query->result();
    }
    function checkLogin($email,$password){
        $where=array(
            "Email"=>$email,
            "Password"=>md5($password),
            "AuthorityName !="=>"Guest"
            );
        $query=$this->db
                ->where($where)
                ->get('adp_oauth'); 
        return $query->result();
    }
    function getAuthoritys($list){
        
        $query=$this->db
        ->from("adp_authority")
        ->where(array(
            "Delete"=>null
            ))
        ->where_in("Keyword",$list)
        ->get();
        return $query->result();
    }
    function gets(){
        $query=$this->db
                ->where("Email !=","khuongxuantruong@gmail.com")
                ->order_by("Position","DESC")
                ->get('adp_oauth'); 
        return $query->result();
    }
    function insert($params){
        $this->db->set('Insert', 'NOW()', FALSE);
        $this->db->insert('adp_oauth', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function update($id,$params){
        $this->db->where('ID', $id);
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->update('adp_oauth', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function changepassword($id,$currentpass="",$newpass=""){
        $params=array(
            "Password"=>md5($newpass)
        );
        $this->db->where(array('ID'=> $id,"Password"=>md5($currentpass)));
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->update('adp_oauth', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function upgraded($ID,$Position){
        $params=array(
            "Position"=>1,
            "AuthorityName"=>"User"
        );
        $this->db->where(array(
            'ID'=> $ID,
            "Position <"=>$Position
            ));
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->update('adp_oauth', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function downgraded($ID,$Position){
        $params=array(
            "Position"=>0,
            "AuthorityName"=>"Guest",
            "Authority"=>Null
        );
        $this->db->where(array(
            'ID'=> $ID,
            "Position <"=>$Position
            ));
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->update('adp_oauth', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function applyAuthority($ID,$authority,$Position){
        $params=array(
            "Authority"=>$authority
        );
        $this->db->where(array(
            'ID'=> $ID,
            "Position <"=>$Position
            ));
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->update('adp_oauth', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
}
?>
